<template>
  <div id="app">
    <router-view :key="pageKey" />
  </div>
</template>

<script>
  import CryptoJS from 'crypto-js'
  import { mapActions, mapGetters } from 'vuex'
  import SystemApi from '@/api/SystemApi'
  import OperateLogApi from '@/api/system/OperateLogApi.js'
  export default {
    data() {
      return {
        pageKey: 1,
        username: '',
        password: ''
      }
    },
    computed: {
      ...mapGetters(['getThemeList', 'getLocale'])
    },
    watch: {
      getLocale() {
        this.pageKey++
      }
    },
    created() {
      // 存在用户信息 解密登录
      const user = this.$route.query.user
      const password = this.$route.query.password
      const time = this.$route.query.time
      const maxTime = 600000 // 大于10分钟就不能使用这个密码了
      if (user && password && time) {
        const t = Number(time) * 1000
        const now = new Date().getTime()
        if ((now - t) > maxTime) return
        const key = ('Hd23#' + t).slice(0, 16)
        // 获取密钥对应的byte数组
        const keyBytes = CryptoJS.enc.Utf8.parse(key)
        // 解密操作
        const decryptedBytes = CryptoJS.AES.decrypt(decodeURIComponent(password), keyBytes, {
          mode: CryptoJS.mode.ECB, // 设置成与后端一致的加解密模式
          padding: CryptoJS.pad.Pkcs7// 设置成与后端一致的填充方式
        })
        const decryptedText = decryptedBytes.toString(CryptoJS.enc.Utf8)
        const result = decryptedText.toString()
        if (result.length) {
          // 输出解密结果
          const pwd = CryptoJS.enc.Utf8.parse(result)
          this.password = CryptoJS.enc.Base64.stringify(pwd)
          this.username = user
          this.login()
        } else {
          console.log('密码错误')
        }
      }

      document.title = this.$config.sysName[this.$i18n.locale] + '-' + this.$config.systemVersion
      if (localStorage.currentNavtype) {
        this.$store.commit('setTabType', localStorage.currentNavtype)
      }
      const theme = localStorage.currentTheme || ''
      // 判断是否设置主题
      if (theme && this.getThemeList.includes(theme)) {
        this.$store.commit('setTheme', theme)
        document.body.setAttribute('data-theme', theme)
      } else {
        localStorage.currentTheme = 'dark-theme'
        this.$store.commit('setTheme', 'dark-theme')
        document.body.setAttribute('data-theme', 'dark-theme')
      }
      if (localStorage.changeSystem === 'true') {
        this.$store.commit('SET_NOTIFY_LIST')
      }
      localStorage.setItem('notifyList', '[]')
      localStorage.setItem('changeSystem', false)
      this.$store.dispatch('loadLanData')
      this.$store.dispatch('loadDict')
    },
    mounted() {
      window.addEventListener('storage', event => {
        if (event.key === 'baseRegionList') {
          this.$store.commit('SET_BASE_REGION_LIST', JSON.parse(event.newValue))
        }
        if (event.key === 'token') {
          if (event.newValue) {
            this.$store.dispatch('loadInfo')
            this.pageKey++
          }
        }
      })
    },
    methods: {
      ...mapActions(['loadInfo']),
      login() {
        const data = {
          username: this.username,
          password: this.password
        }
        SystemApi.login(data).then(result => {
          if (result) {
            localStorage.setItem('token', result)
            this.loadInfo(true).then(res => {
              // 记录登录日志
              try {
                OperateLogApi.addLog(
                  {
                    api: '/authen/login',
                    item: 'LOGIN', // 登录
                    terminal: 'LOGIN', // 登录,
                    operateResult: true,
                    object: '/',
                    content: JSON.stringify({
                      userName: data.username,
                      sysName: this.$config.sysName[this.$i18n.locale],
                      i18nKey: 'LOGIN_LOG'
                    })
                  }
                )
              } catch (err) {
                console.log(err)
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss">

</style>
